package com.webchat.client.mall.controller.im;


import com.webchat.common.config.annotation.ValidateLogin;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.mall.im.MallChatRecommendAnswerReqVO;
import com.webchat.rmi.ugc.mall.im.MallAiBotClient;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/mall-service/im/ai")
public class MallAiBotController {


    @Autowired
    private HttpServletResponse response;

    @Autowired
    private MallAiBotClient mallAiBotClient;

    @Autowired
    private WebClient ugcWebClient;

    /**
     * 智能话回复话术推荐
     *
     * @return
     */
    @ValidateLogin
    @PostMapping(value = "/recommend/answers", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> recommendAnswers(@RequestBody MallChatRecommendAnswerReqVO chatRecommendAnswerReq) {

        // 响应字符集编码设置，否则可能出现中文乱码
        response.setCharacterEncoding("UTF-8");
        // 设置商家信息
        chatRecommendAnswerReq.setAccount(SessionHelper.getCurrentUserId());
        return ugcWebClient.post()
                .uri("/ugc-service/mall/im/recommend/answer")
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(chatRecommendAnswerReq)
                .accept(MediaType.TEXT_EVENT_STREAM)
                .retrieve()
                .bodyToFlux(String.class);
    }

//    @ValidateLogin
    @GetMapping(value = "/recommend/answers", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> recommendAnswers() {

        MallChatRecommendAnswerReqVO chatRecommendAnswerReq = new MallChatRecommendAnswerReqVO();
        // 响应字符集编码设置，否则可能出现中文乱码
        response.setCharacterEncoding("UTF-8");
        // 设置商家信息
        chatRecommendAnswerReq.setAccount("U_1bb8a10f2d2a45a4b075c20016f10cb8");
        chatRecommendAnswerReq.setChatAccount("U_37ed910debfe42e2beda976f4b3dd0d2");
        chatRecommendAnswerReq.setRecommendSize(3);

        return ugcWebClient.post()
                .uri("/ugc-service/mall/im/recommend/answer")
                .contentType(MediaType.APPLICATION_JSON)
                .bodyValue(chatRecommendAnswerReq)
                .accept(MediaType.TEXT_EVENT_STREAM)
                .retrieve()
                .bodyToFlux(String.class);
    }

}
